package cn.tedu.practice2303.LSS.day0412homework;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class Client {
    private Socket socket1;
    public Client() {
        try {
            System.out.println("正在连接服务器...");
            socket1 = new Socket("localhost",8088);
            System.out.println("与服务器连接成功.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void start1() {
        try {
            OutputStream out = socket1.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw, true);
            Scanner scan = new Scanner(System.in);
            String line;
            while (true) {
                line = scan.nextLine();
                if ("exit".equalsIgnoreCase(line)) {
                    break;
                }
                pw.println(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try{
                socket1.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }

    }
    public static void main(String[] args) throws IOException {
        Client client = new Client();
        client.start1();
    }
}
